package org.javaweb.cms_web.controller;

import org.javaweb.cms_web.model.Enum.UserStatus;
import org.javaweb.cms_web.model.User;
import org.javaweb.cms_web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ApplicationCheckController {

    @Autowired
    private UserService userService;

    @GetMapping("/application-check")
    public String applicationCheck() {
        return "applicationCheck";
    }

    @PostMapping("/application-check")
    public String applicationCheck(@RequestParam String email, Model model) {

        User user = userService.getUserByEmail(email);
        if (user == null) {
            model.addAttribute("error", "该邮箱未注册");
            return "applicationCheck";
        }

        if(user.getStatus() == UserStatus.PENDING_REVIEW.getCode()) {
            model.addAttribute("message", "该账号正在审核中");
        }
        else if (user.getStatus() == UserStatus.CANCELLED.getCode()) {
            model.addAttribute("error", "该账号已注销");
        }
        else if (user.getStatus() == UserStatus.NOT_PASSED.getCode()) {
            model.addAttribute("error", "该账号审核未通过");
        }
        else {
            model.addAttribute("message", "该账号审核已通过");
        }

        return "applicationCheck";
    }
}
